package ar.edu.uba.fi.jmit.graphvisualizer.server.mock;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import ar.edu.uba.fi.jmit.graphvisualizer.client.DynamicMenuService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class DynamicMenuServiceMock extends RemoteServiceServlet implements DynamicMenuService {
	
	private Random rand = new Random();
	private List<String> apps = new LinkedList<String>();
	private List<String> mons = new LinkedList<String>();

	public DynamicMenuServiceMock() {
		
		apps.add("Application 1");
		apps.add("Application 2");
		apps.add("Application 3");
		apps.add("Application 4");
		
		mons.add("Monitor 1");
		mons.add("Monitor 2");
		mons.add("Monitor 3");
		mons.add("Monitor 4");
		mons.add("Monitor 5");
	}
	
	@Override
	public Boolean hasUpdate(Map<String, List<String>> applicationMonitorsClient) {
		return rand.nextBoolean();
	}
	
	@Override
	public Map<String, List<String>> tree() {
		int cantApps = rand.nextInt(4);
		
		Map<String, List<String>> appMons = new HashMap<String, List<String>>();
		for (int i = 0; i < cantApps; i++) {
			appMons.put(apps.get(i), new LinkedList<String>());
			int cantMons = rand.nextInt(4) + 1;
			for (int j = 0; j < cantMons; j++)
				appMons.get(apps.get(i)).add(mons.get(j));
		}
		
		return appMons;
	}

	@Override
	public Map<String, Serializable> getData(String appName, String monName) {
		return getDataFieldValue(appName, monName);
	}
	
	@SuppressWarnings("unused")
	private Map<String, Serializable> getDataAverageMethodTime(String appName, String monName) {
		Map<String, Serializable> map = new HashMap<String, Serializable>();
		map.put("monitor_name", "Test");
		map.put("monitored_class", "ar.edu.uba.fi.Mock");
		map.put("client_application", "MockApplication");
		map.put("jmit.monitorengine.methodTime.monitoredMethod", "mockMethod(..)");
		map.put("monitor_type", "AVERAGE_METHOD_TIME");
//		map.put("jmit.monitorengine.methodTime.elapsedTime", i % 5);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.key.date", new Date());
		return map;
	}
	
	@SuppressWarnings("unused")
	private Map<String, Serializable> getDataMethodTime(String appName, String monName) {
		Map<String, Serializable> map = new HashMap<String, Serializable>();
		map.put("monitor_name", "Test");
		map.put("monitored_class", "ar.edu.uba.fi.Mock");
		map.put("client_application", "MockApplication");
		map.put("jmit.monitorengine.methodTime.monitoredMethod", "mockMethod(..)");
		map.put("monitor_type", "METHOD_TIME");
//		map.put("jmit.monitorengine.methodTime.elapsedTime", i % 5);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.key.date", new Date());
		return map;
	}
	
	@SuppressWarnings("unused")
	private Map<String, Serializable> getDataObjectCount(String appName, String monName) {
		Map<String, Serializable> map = new HashMap<String, Serializable>();
		map.put("monitor_name", "Test");
		map.put("monitored_class", "ar.edu.uba.fi.Mock");
		map.put("client_application", "MockApplication");
		map.put("monitor_type", "OBJECT_COUNT");
//		map.put("jmit.monitorengine.methodTime.elapsedTime", i % 5);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.key.date", new Date());
		return map;
	}
	
	@SuppressWarnings("unused")
	private Map<String, Serializable> getDataMethodInvocationCount(String appName, String monName) {
		Map<String, Serializable> map = new HashMap<String, Serializable>();
		map.put("monitor_name", "Test");
		map.put("monitored_class", "ar.edu.uba.fi.Mock");
		map.put("client_application", "MockApplication");
		map.put("monitor_type", "METHOD_INVOCATION_COUNTER");
		map.put("jmit.monitorengine.methodInvocation.signature", "methodMock(..)");
//		map.put("jmit.monitorengine.methodTime.elapsedTime", i % 5);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.key.date", new Date());
		return map;
	}
	
	private Map<String, Serializable> getDataFieldValue(String appName, String monName) {
		Map<String, Serializable> map = new HashMap<String, Serializable>();
		map.put("monitor_name", "Test");
		map.put("monitored_class", "ar.edu.uba.fi.Mock");
		map.put("client_application", "CarMarket");
		map.put("monitor_type", "FIELD_VALUE");
//		map.put("jmit.monitorengine.methodTime.elapsedTime", i % 5);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.methodTime.average", i % 3);
//		map.put("jmit.monitorengine.key.date", new Date());
		return map;
	}
	
}
